package com.wk.playersdk.util;

import android.app.Activity;
import android.content.Context;
import android.content.res.Configuration;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Environment;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.view.WindowManager;

import java.io.File;

/**
 * @author Hnyer
 * 跟系统参数有关的 帮助类
 */
public class WkDeviceInforTool
{
	/****采用 饿汉单例模式，防止在生成大量的临时变量，频繁gc影响性能**/
	public static WindowManager myWindowManager ;
	public static DisplayMetrics myDisplayMetrics ;

	public static WindowManager myGetWindowManageger(Context context)
	{
		if(myWindowManager==null){
			myWindowManager = (WindowManager) (context.getSystemService(Context.WINDOW_SERVICE));
		}
		return myWindowManager;
	}
	private static DisplayMetrics myGetDisplayMetrics( )
	{
		if(myDisplayMetrics==null){
			myDisplayMetrics = new DisplayMetrics();
		}
		return myDisplayMetrics;
	}


	/**
	 * 获取屏幕的高度 适合在activity中调用
	 * @param mActivity mActivity
	 * @return getHeight
	 */
	public static int getHeight(Activity mActivity)
	{
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(myGetDisplayMetrics());
		return  myGetDisplayMetrics().heightPixels;

	}

	/**
	 * 获取屏幕的宽度 适合在activity中调用
	 * @param mActivity Activity
	 * @return getWidth
	 */
	public static int getWidth(Activity mActivity)
	{
		mActivity.getWindowManager().getDefaultDisplay().getMetrics(myGetDisplayMetrics());
		return  myGetDisplayMetrics().widthPixels;
	}

	/**
	 * 适合在自定义控件中调用
	 * @param context Context
	 * @return getWindowWidth
	 */
	public static int getWindowWidth(Context context)
	{
		myGetWindowManageger(context).getDefaultDisplay().getMetrics(myGetDisplayMetrics());
		return  myGetDisplayMetrics().widthPixels;
	}


	/**
	 * 适合在自定义控件中调用
	 * @param context Context
	 * @return getWindowHeigh
	 */
	public static int getWindowHeigh(Context context)
	{
		myGetWindowManageger(context).getDefaultDisplay().getMetrics(myGetDisplayMetrics());
		return myGetDisplayMetrics().heightPixels;
	}



	public static int getColorValue(Context mCtx , int colorId)
	{
		return ContextCompat.getColor(mCtx, colorId) ;
	}


	/**
	 * 判断是否存在sd卡
	 */
	public static boolean isExistSDCard()
	{
		return  Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) ;
	}

	/**
	 * 获得 SD卡路径
	 * @return File
	 */
	public static File getExternalSdPath()
	{
		return Environment.getExternalStorageDirectory();
	}

	/**
	 * 获取内置Sd卡路径
	 * @return File
	 */
	public static File getInternalSdPath()
	{
		return  Environment.getDataDirectory();
	}





	/**
	 * 根据Dp 获取px
	 * @param mctx Context
	 * @param dp dp
     * @return pix
     */
	public static  int getPixByDp(Context mctx   , int dp)
	{
		DisplayMetrics displayMetrics = mctx.getResources().getDisplayMetrics();
		return (int)(dp*displayMetrics.density);
	}






	/**
	 * 获取当前程序的 包名
	 * @param context  Context
	 * @return   PackageName
	 */
	public static String getAppPackgeName(Context context)
	{
		return  context.getPackageName() ;
	}









	/**
	 * 获取手机操作系统的当前版本
	 * @return    currentapiVersion
	 */
	public static int getDeviceCurrentVersion()
	{
		return   android.os.Build.VERSION.SDK_INT ;
	}


	/**
	 * 判断当前activity处于横屏还是竖屏状态
	 * @param mCtx Context
	 * @return true  ORIENTATION_LANDSCAPE
	 */
	public static boolean isDeviceAtLandScapeOrientation( Context mCtx)
	{
		return  mCtx.getResources().getConfiguration().orientation== Configuration.ORIENTATION_LANDSCAPE;
	}





	/**
	 * 判断网络是否可用<br>
	 * @param activity Activity
	 * @return true 可用
	 */
	public static boolean isNetworkAvailable(Activity activity)
	{
		// 获取手机所有连接管理对象（包括对wi-fi,net等连接的管理）
		ConnectivityManager connectivityManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (connectivityManager == null) {
			return false;
		} else {
			// 获取NetworkInfo对象
			NetworkInfo[] networkInfo = connectivityManager.getAllNetworkInfo();
			if (networkInfo != null && networkInfo.length > 0)
			{
				for(NetworkInfo info : networkInfo)
				{
					// 判断当前网络状态是否为连接状态
					if (info.getState() == NetworkInfo.State.CONNECTED)
					{
						return true;
					}
				}
			}
		}
		return false;
	}//


	/**
     保持屏幕常亮。<br>
	 只影响当期APP，app退出后 就会执行系统的默认设置。<br>
     方法2：在activity对应的xml中配置 android:keepScreenOn="true"
	 */
	public static void keepScreenOn(Activity  mActivity)
	{
		mActivity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
	}

	/***
	 * 取消屏幕常亮设置。<br>
	 *     只影响当期Activity
	 */
	public static void clearKeepScreenOn(Activity  mActivity)
	{
		mActivity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON) ;
	}

}
